const path=require('path');
const Koa = require('koa');
const staticServer = require('koa-static');

const app = new Koa();

console.log(path.join(__dirname, 'www'));
//静态文件
app.use(staticServer(path.join(__dirname, 'www'))); 

//错误处理
app.use(async (ctx, next) => {
	var start = new Date;
	try{
		await next();
		if ((typeof ctx.body) === 'object' )  {
			if ((typeof ctx.body.data) === 'undefined') {
				ctx.body.data = ctx.body;
			}
			if ((typeof ctx.body.code) === 'undefined') {
				ctx.body.code = 0;
			}
			if ((typeof ctx.body.msg) === 'undefined') {
				ctx.body.msg = '';
			}
		}
	}
	catch(e){
		ctx.body = {
			'code': 500,
			'data': null,
			'msg': '服务异常'
		};
		console.log(e);
	}
	
	var ms = new Date - start;
	ctx.set('X-Response-Time', ms + 'ms');
});

app.use( async (ctx, next) => {
    ctx.body = 'hello world!';
});

app.listen(13000);
console.log('[demo] start-quick is starting at port 13000');